package algorithm.stack;

public class MyStack {
    //栈的大小
    private final int maxSize;
    //实现栈的数组
    private final float[] stackArray;
    //栈顶
    private int top = -1;

    //构造器
    public MyStack(int maxSize) {
        this.maxSize = maxSize;
        stackArray = new float[maxSize];
    }

    //栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //栈空
    public boolean isEmpty() {
        return top == -1;
    }

    //入栈
    public void push(float val) {
        if (isFull()) {
            System.out.println("栈满，操作失败！");
            return;
        }
        stackArray[++top] = val;
    }

    //出栈
    public float pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，出栈失败");
        }
        float val = stackArray[top];
        top--;
        return val;
    }

    //查看栈顶元素
    public float peek() {
        return stackArray[top];
    }

    //遍历栈
    public void show() {
        if (isEmpty()) {
            System.out.println("栈空，打印失败！");
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("%d\t", stackArray[i]);
            System.out.println();
        }
    }

    //定义符号的优先级
    //+-为0
    //*/为1
    public static int priority(char oper) {
        if (oper == '+' || oper == '-') {
            return 0;
        } else{
            return 1;
        }
    }
    //判断输入的字符是否合法

    /**
     *
     * @param ch
     * @return 0表示是字符，1表示是数字
     */
    public static int chType(char ch){
        String s = "0123456789";
        //是否为字符
        if (ch == '+'||ch == '-'||ch=='*'||ch=='/'){
            return 0;
        }
        //是否为数字
        String temp = String.valueOf(ch);
        if (s.contains(temp)){
            return 1;
        }
        //如果都不是
        throw new RuntimeException("非法字符");
    }

    //计算过程
    public static float calcu(float num1, float num2, char op) {
        float val=0;
        switch (op){
            case '+':
                val = num1+num2;
                break;
            case '-':
                val = num2-num1;
                break;
            case '*':
                val = num1*num2;
                break;
            case '/':
                val = num2/num1;
                break;
            default:break;
        }
        return val;
    }

}
